package com.xinchuang.mapper.targetSystem;

import com.xinchuang.entity.targetSystem.target.SystemType;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @ClassName SystemTypeMapper
 * @Description: TODO(写一段描述的话)
 * @Author zhangxuan
 * @Date 2022-03-23 17:03
 * @Verion V1.0
 */
@Repository
public interface SystemTypeMapper {

    @Insert(value = {
            "INSERT INTO system_type (id,type_name,type_id,status,del) ",
            "VALUES ",
            "(#{id},#{typeName},#{typeId},#{status},1)",
            "ON DUPLICATE KEY UPDATE type_name = #{typeName}, type_id = #{typeId}, status = #{status}"
    })
    int addSystemTypeAndId(SystemType systemType);

    @Delete(value = "UPDATE system_type SET del = 0 WHERE id = #{id}")
    int deleteSystemType(@Param("id") String id);

    @Delete(value = "UPDATE system_type SET del = 0 WHERE type_id = #{id}")
    int deleteSystemTypeByTypeId(@Param("type_id") String id);

    @Select(value = "SELECT id,type_name,type_id,status FROM system_type WHERE status = #{status} AND del = 1 ORDER BY create_time DESC")
    @Results(value = {
            @Result(property = "typeName", column = "type_name"),
            @Result(property = "typeId", column = "type_id")
    })
    List<SystemType> selectSystemTypeList(@Param("status") Integer status);

    @Select(value = "SELECT id,type_name,type_id,status FROM system_type WHERE type_id = #{typeId} AND status = #{status} AND del = 1 ORDER BY create_time DESC")
    @Results(value = {
            @Result(property = "typeName", column = "type_name"),
            @Result(property = "typeId", column = "type_id")
    })
    List<SystemType> selectSystemTypeByTypeIdList(@Param("typeId") String typeId, @Param("status") Integer status);

    @Select(value = "SELECT id,type_name,type_id,status FROM system_type WHERE id = #{id} LIMIT 1")
    @Results(value = {
            @Result(property = "typeName", column = "type_name"),
            @Result(property = "typeId", column = "type_id")
    })
    SystemType selectSystemTypeOnly(@Param("id") String id);

    @Select(value = "SELECT id FROM system_type WHERE type_id = #{name} AND del = 1 AND status = #{status} LIMIT 1")
    String selectIdByName(@Param("name") String name, @Param("status") Integer status);

    @Select(value = "SELECT COUNT(1) FROM system_type WHERE type_name = #{name} AND del = 1 AND status = #{status} LIMIT 1")
    Integer selectCountByTypeName(@Param("name") String name, @Param("status") Integer status);

    @Select(value = "SELECT COUNT(1) FROM system_type WHERE type_name != #{name} AND del = 1 AND status = #{status} LIMIT 1")
    Integer selectCountNoNameByTypeName(@Param("name") String name, @Param("status") Integer status);

    @Select(value = "SELECT type_name FROM system_type WHERE id = #{id} LIMIT 1")
    String getSystemName(@Param("id") String sId);

    @Select(value = "SELECT type_id FROM system_type WHERE id = #{id} LIMIT 1")
    String getSystemsName(@Param("id") String sysId);

}
